import { USERS } from "./users";

// T Template的缩写
interface Result<T> {
  data: T | null;
  error: string | null;
}
interface User {
  username: string;
}

function fetch<T>(url: "users" | "products"): Result<T> {
  if (url === "users") {
    // 自己过滤掉多余的属性
    const users:User[] = USERS.map(user => ({username: user.username}))
    return { data:users as T, error: null };
  }
  return { data: null, error: null };
}

let users = fetch<User[]>("users").data;
console.log(users);
/**
[ { username: 'Tony' }, { username: 'Jony' } ]
 */
